using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : AbstractController
{
    private IPlayer m_Player;
    public IPlayer Player => m_Player;
    public List<IPlayer> players = new List<IPlayer>();
    public List<IPlayerPet> pets = new List<IPlayerPet>();
    private bool isHostPlayerInit;
    
    public PlayerController()
    {
        // 移除聯機邏輯
    }
    
    protected override void AlwaysUpdate()
    {
        base.AlwaysUpdate();
        // 移除聯機邏輯
    }
    
    protected override void Init()
    {
        base.Init();
        if (ModelContainer.Instance.GetModel<SceneModel>().sceneType == SceneType.Battle)
        {
            if (ModelContainer.Instance.GetModel<MemoryModel>().PlayerAttr != null)
            {
                SetPlayer(ModelContainer.Instance.GetModel<MemoryModel>().PlayerAttr);
            }
            else
            {
                SetPlayer(AttributeFactory.Instance.GetPlayerAttr(PlayerType.Knight));
            }
        }
    }
    
    protected override void OnAfterRunInit()
    {
        base.OnAfterRunInit();
    }
    
    protected override void OnAfterRunUpdate()
    {
        base.OnAfterRunUpdate();
        if (!isHostPlayerInit && m_Player != null)
        {
            isHostPlayerInit = true;
            foreach (GameObject obj in GameObject.FindGameObjectsWithTag("Player"))
            {
                if (obj.transform.parent.gameObject != m_Player.gameObject && obj.transform.parent.name != "Templar" && obj.transform.parent.name != "Knight")
                {
                    obj.transform.parent.transform.Find("Collider").GetComponent<CapsuleCollider2D>().isTrigger = false;
                    if (obj.transform.parent.GetComponent<Rigidbody2D>() != null)
                    {
                        obj.transform.parent.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Static;
                    }
                }
            }
            CameraUtility.Instance.SetFollow(m_Player.gameObject.transform);
            UnityTool.Instance.GetComponentFromChild<CapsuleCollider2D>(m_Player.gameObject, "Collider").isTrigger = false;
        }
        if (m_Player != null)
        {
            m_Player.m_Input = ModelContainer.Instance.GetModel<PlayerInputModel>().m_InputPack;
            m_Player.GameUpdate();
            foreach (IPlayer player in players)
            {
                player.GameUpdate();
            }
        }
        foreach (IPlayerPet pet in pets)
        {
            pet.GameUpdate();
        }
    }
    
    public void SetPlayer(PlayerAttribute attr)
    {
        m_Player = PlayerFactory.Instance.GetPlayer(attr.m_ShareAttr.Type, attr);
        if (ModelContainer.Instance.GetModel<SceneModel>().sceneName == SceneName.MiddleScene)
        {
            m_Player.gameObject.SetActive(true);
        }
        if (ModelContainer.Instance.GetModel<SceneModel>().sceneName == SceneName.BattleScene)
        {
            m_Player.m_Attr.isRun = false;
        }
    }
    
    public void AddPlayer(PlayerAttribute attr, string playerName)
    {
        players.Add(PlayerFactory.Instance.GetPlayer(attr.m_ShareAttr.Type, attr));
        players[players.Count - 1].gameObject.GetComponent<CapsuleCollider2D>().isTrigger = false;
        players[players.Count - 1].UserName = playerName;
    }
    
    public void AddPet(PetType type, IPlayer player)
    {
        pets.Add(PlayerFactory.Instance.GetPlayerPet(type, player));
        if (SceneModelCommand.Instance.GetActiveSceneName() == SceneName.MiddleScene)
        {
            pets.ForEach(x => x.gameObject.SetActive(true));
        }
    }
    
    public void AddPet(IPlayerPet pet)
    {
        pets.Add(pet);
        if (SceneModelCommand.Instance.GetActiveSceneName() == SceneName.MiddleScene)
        {
            pets.ForEach(x => x.gameObject.SetActive(true));
        }
    }
    
    public IEnemy GetCloseEnemy(GameObject origin)
    {
        float min = 15;
        GameObject o = null;
        foreach (GameObject obj in GameObject.FindGameObjectsWithTag("Enemy"))
        {
            if (Vector2.Distance(origin.transform.position, obj.transform.position) < min)
            {
                min = Vector2.Distance(origin.transform.position, obj.transform.position);
                o = obj;
            }
        }
        if (o == null)
        {
            return null;
        }
        return o.GetComponent<Symbol>().GetCharacter() as IEnemy;
    }
}
